package sf.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;

import sf.model.DBColumnInfo;
import sf.model.fields.Field;
import sf.model.fields.FieldFactory;

public class DetailsPane extends JPanel {

	private static final long serialVersionUID = 1L;

	private StandardForm parentForm;

	private JPanel panelFields;
	private JToolBar toolBar;

	private int labBr = 0, fieldBr = 0;

	private List<DBColumnInfo> columns;

	private HashMap<String, JComponent> fields;

	public DetailsPane(StandardForm parentForm) {
		this.parentForm = parentForm;

		init();
		
		int mode = parentForm.getMode();
		if ((mode & StandardForm.MODE_EDIT) == 0) {
			parentForm.getToolbar().getSearchField().setText(null);

			clearAllFields();
			unlockAllFields();
			focusFirstField();

			toolBar.removeAll();
			
			if ((mode & StandardForm.MODE_ADD) != 0) {
				toolBar.add(parentForm.getActionManager().getPotvrdaUnosaAction());
				toolBar.add(parentForm.getActionManager().getPonistiAction());
			}
			else if ((mode & StandardForm.MODE_SEARCH) != 0) {
				toolBar.add(parentForm.getActionManager().getPotvrdaPretrageAction());
				toolBar.add(parentForm.getActionManager().getPonistiAction());
			}
		}
	}

	private void init() {
		setLayout(new BorderLayout());

		columns = parentForm.getBrowser().getTableColumns();

		fields = new HashMap<String, JComponent>();

		setupFieldsPane();
		setupToolbar();

		add(panelFields, BorderLayout.CENTER);
		add(toolBar, BorderLayout.EAST);
	}

	private void setupFieldsPane() {
		panelFields = new JPanel();
		panelFields.setBorder(new EmptyBorder(10, 5, 10, 5));
		panelFields.setLayout(new GridBagLayout());

		for (DBColumnInfo column : columns) {
			String naziv = column.getLabel();
			JLabel label = new JLabel(naziv != null ? naziv + ": " : "",
					JLabel.TRAILING);

			panelFields.add(label, createLabelConst());

			int columnSize = column.getSize();
			Class<?> cls = column.getCls();

			// izbacio sam iz DBColumnInfo promenljivu foreignKey posto ja o
			// tome ne vodim racuna vise
			// valjda ...

			JComponent field = FieldFactory.getFieldForClass(cls, naziv,
					columnSize, parentForm.getController()
							.getFieldFocusListener());
			field.setEnabled(!column.isPrimaryKey());
			panelFields.add(field, createFieldConst());
			fields.put(naziv, field);
		}

		// XXX Proba za zooom
//		HashMap<String, AbstractField> zoomFields;
//		ZoomFieldFactory factory = new ZoomFieldFactory();
//		zoomFields = factory.getFields(parentForm.getContext().getTable()
//				.toUpperCase());
//		for (String fkey : zoomFields.keySet()) {
//			AbstractField f = zoomFields.get(fkey);
//			JLabel label = new JLabel(f.getLabelText(), JLabel.TRAILING);
//			panelFields.add(label, createLabelConst());
//			panelFields.add(f, createFieldConst());
//			fields.put(fkey, f);
//		}

	}

	private GridBagConstraints createLabelConst() {
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = labBr++;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = GridBagConstraints.EAST;
		gridBagConstraints.insets = new Insets(5, 0, 5, 5);

		return gridBagConstraints;
	}

	private GridBagConstraints createFieldConst() {
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = fieldBr++;
		gridBagConstraints.anchor = GridBagConstraints.WEST;

		return gridBagConstraints;
	}

	private void setupToolbar() {
		toolBar = new JToolBar(JToolBar.VERTICAL);
		toolBar.setRollover(true);
		toolBar.setFloatable(false);

		toolBar.add(parentForm.getActionManager().getPotvrdaIzmeneAction());
		toolBar.add(parentForm.getActionManager().getPonistiAction());
	}

	public Object getFieldValue(String fieldName) {
		Object value = null;
		JComponent comp = fields.get(fieldName);
		value = ((Field) comp).getValue();

		return value;
	}

	public void setFieldValue(String fieldName, Object value) {
		JComponent comp = fields.get(fieldName);
		((Field) comp).setValue(value);
	}

	public LinkedHashMap<String, Object> getFieldsAndValues() {
		LinkedHashMap<String, Object> list = new LinkedHashMap<String, Object>();

		for (DBColumnInfo column : columns) {
			String code = column.getCode();
			Object value = getFieldValue(column.getLabel());
			list.put(code, value);
		}

		return list;
	}

	public void lockPKFields() {
		for (DBColumnInfo column : columns)
			fields.get(column.getLabel()).setEnabled(!column.isPrimaryKey());
	}

	public boolean checkNullable() {
		boolean ok = true;
		for (DBColumnInfo column : columns)
			if (getFieldValue(column.getLabel()) == null
					&& !column.isNullable()) {
				ok = false;
				break;
			}

		return ok;
	}

	public void unlockAllFields() {
		for (JComponent comp : fields.values())
			comp.setEnabled(true);
	}

	public void clearAllFields() {
		for (JComponent comp : fields.values())
			((Field) comp).clear();
	}

	public void focusFirstField() {
		for (Component comp : panelFields.getComponents())
			if (comp instanceof Field && comp.isEnabled()) {
				comp.requestFocus();
				break;
			}
	}

	public JToolBar getToolbar() {
		return toolBar;
	}
}
